package com.cresch.ical.downloader;

import javax.net.ssl.HandshakeCompletedListener;

import org.apache.http.client.ResponseHandler;

import com.cresch.ical.downloader.ical.Calendar;
import com.cresch.ical.downloader.network.HTTPRequestHelper;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class Main extends Activity {
	
	private EditText URL;
	private Button StartButton;
	private Button buttonClose;
	
	private ProgressDialog progressDialog;
	private Calendar calendar;
	
	public static final String CALENDAR_RAW_DATA = "CALENDAR_RAW_DATA";
	
	// use a handler to update the UI (send the handler messages from other threads)
    private final Handler responseHandler = new Handler() {

        @Override
        public void handleMessage(final Message msg) {
        	if (progressDialog != null) {        		
        		progressDialog.dismiss();
        	}
            String rawCalendar = msg.getData().getString("RESPONSE");
            handleResponseData(rawCalendar);            
        }
    };
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        /* connect with UI elements */
        this.URL = (EditText) findViewById(R.id.URLText);
        this.StartButton = (Button) findViewById(R.id.Start); 
        
        this.URL.setText("http://elearning-mm.fh-joanneum.at/icals/SWD06.ics");        
        this.StartButton.setOnClickListener(new OnClickListener(){
        	public void onClick(final View v) {
        		LoadURL();
        	}
        });
        
        buttonClose = (Button) findViewById(R.id.Close);
        buttonClose.setOnClickListener(new OnClickListener(){
        	public void onClick(final View v) {
        		finish();
        	}
        });
        
        this.calendar = new Calendar();
        
    }
	
	private void LoadURL() {
		this.progressDialog = ProgressDialog.show(this, "Step 1 . . .", "downloading ical file");
		
		final ResponseHandler<String> responseHandler = HTTPRequestHelper.getResponseHandlerInstance(this.responseHandler);
		final String URL = this.URL.getText().toString();
		
		// lets handle this thread the download
        final Thread URLReaderThread = new Thread() {
        	public void run() {
        		
        		HTTPRequestHelper helper = new HTTPRequestHelper(responseHandler);
        		helper.performGet(URL, null, null, null);
        	}
        };
        URLReaderThread.start();
	}
     
	private void handleResponseData(String rawCalendar) {
		
		Intent calendarView = new Intent(this, CalendarView.class);
		
		Bundle data = new Bundle();
		
		data.putString(Main.CALENDAR_RAW_DATA, rawCalendar);		
		calendarView.putExtras(data);		      
		this.startActivityForResult(calendarView, 0);
	}
}